//
// Created by F1997 on 2024/12/24.
//

#include "CompareTraffic.h"

namespace ntp {

    void CompareTraffic::sampledAndSave(std::shared_ptr<spdlog::logger> _my_logger, IniReader* iniCfg) {

        std::string fileName = "save.txt";

        //FILE *saveFile;
        //saveFile = fopen(fileName.c_str(), "wt+");

        std::ofstream file(fileName);
        // 检查文件是否成功打开
        if (!file.is_open()) {
            _my_logger->info("文件打开失败: {}",fileName);
            std::cerr << "文件打开失败: " << fileName << std::endl;
            return;
        }

        int i = 0;
        while (true) {
            // 等待采样间隔
            std::this_thread::sleep_for(std::chrono::milliseconds(3000));
            // 计算百分比
            if (1 > 0) {


                //double percentage = static_cast<double>(a_val) / (a_val + b_val) * 100.0;
                double percentage = static_cast<double>(1) / ( 1) * 1.0; // 100.0
                //std::cout << "第 " << i << " 次统计，a/b: " << percentage << "%, " << a_val << " = " << b_val << "\n";
                //file << "第 " << i << " 次统计，a/b: " << percentage << "% " << " index: " << in << " tm_dff(a-b): " << tm_dff << "" << std::endl; // 强制刷写 //<< a_val << " = " << b_val << "\n";
                file << "第 " << i << " 次统计，a/b: " << percentage << "%" << " tm_dff(a-b): " << 1 << "" << std::endl; // 强制刷写 //<< a_val << " = " << b_val << "\n";

            } else {
                //std::cout <<  "第 " << i << " 次统计。没有获取到数据。" << a_val << " = " << b_val << "\n";
                file << "第 " << i << " 次统计。没有获取到数据。" << std::endl; //<< a_val << " = " << b_val << "\n";
            }
            //file.flush();
        }
        file.flush();

    }
} // ntp